﻿using Spider.Entitys.Spider;
using SpiderCore.DB;
using SpiderCore.Downloader;
using SpiderCore.Engines;
using SpiderCore.Pipeline;
using SpiderCore.Scheduler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SpiderCore.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            IDB db = new RedisDb();
            try
            {

                //string url = "https://www.xiashu.la/type/nan_0_0_allvisit_1.html";
                //string encoding = "utf-8";
                //var request = new RequestModel
                //{
                //    Encoding = encoding,
                //    Url = url,
                //    Timeout = 10000,
                //    UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36",
                //    Sleep = 2000,
                //    Depth = 1,
                //    Source = "下书网",
                //    Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
                //    //Connection = "keep-alive",
                //    Header = new List<KeyValue>()
                //    {
                //        new KeyValue("Accept-Encoding","gzip"),
                //        new KeyValue("Accept-Language","zh-CN,zh;q=0.9"),
                //        new KeyValue("Cache-Control","max-age=0"),
                //    },
                //        Cookie = new List<KeyValue>()
                //        {

                //        }
                //    };
                //HttpDownloader hd = new HttpDownloader();
                //var html = hd.Download(request);


                #region 初始化URL匹配规则
                List<CustomRuleModel> customList = new List<CustomRuleModel>()
                {
                    new CustomRuleModel()
                    {
                        Type = 1,
                        Remark = "详情页",
                        UrlAnalysis = @"https://www.xiashu.la/\d+?/",
                        Seq = 99998
                    },
                    new CustomRuleModel()
                    {
                        Type = 2,
                        Remark = "小说文章页面",
                        UrlAnalysis = "https://www.xiashu.la/.+?/.+?.html",
                        Seq = 99997
                    },
                    new CustomRuleModel()
                    {
                        Type = 3,
                        Remark = "按照作者分类",
                        UrlAnalysis = "https://www.xiashu.la/author/.+?",
                        Seq = 13
                    },
                    new CustomRuleModel()
                    {
                        Type = 4,
                        Remark = "分类",
                        UrlAnalysis = "https://www.xiashu.la/type/.+?",
                        Seq = 12
                    },
                    new CustomRuleModel()
                    {
                        Type = 5,
                        Remark = "首页",
                        UrlAnalysis = "https://www.xiashu.la",
                        Seq = 99999
                    },
                };
                #endregion

                ISpiderEngine spiderEngine = new OneSpiderEngine(customList,5);

                #region 初始化下载器工厂
                spiderEngine.InitFactoryDownloader((rule) =>
                {
                    if (rule != null)
                    {
                        switch (rule.Remark)
                        {
                            case "详情页":
                                return new WebDriverDownloader();
                            default:
                                return new HttpDownloader();
                        }
                    }
                    return new HttpDownloader();
                });
                #endregion

                //爬虫初始化
                SpiderInit();

                ///插入处理管道
                //MongoDB持久化  处理
                spiderEngine.AddPipelineHandle(new PipelineHandlerMongoDB());

                //启动抓取引擎，自我循环运行
                spiderEngine.Start();

                while (true)
                {
                    Thread.Sleep(10000);
                }
            }
            catch (Exception ex)
            {

            }
        }

        public IDownloader FactoryDownloader(CustomRuleModel rule)
        {
            if (rule != null)
            {
                switch (rule.Remark)
                {
                    case "详情页":
                        return new WebDriverDownloader();
                    default:
                        return new HttpDownloader();
                }
            }
            return new HttpDownloader();
        }

        private static void SpiderInit()
        {
            var args = new string[] { "https://www.xiashu.la/", "utf-8", "1000" };
            ///初始化任务调度器
            InitScheduler(args);

            ///冲消息队列中国获取消息
            var request = SchedulerManage.Instance.Pop();
            if (request == null)
            {
                request = GetParamRequest(args);
            }
            SchedulerManage.Instance.Push(request);
            //插入到 调度器中

        }

        /// <summary>
        /// 初始化任务调度器
        /// </summary>
        /// <param name="args"></param>
        private static void InitScheduler(string[] args)
        {
            var url = args[0];
            var uri = new Uri(url);
            SchedulerManage.Instance.InitdomainList(new List<string>() { uri.Host });
        }

        private static RequestModel GetParamRequest(string[] args)
        {
            if (args == null || args.Length < 2) return null;

            var url = args[0];
            var encoding = args[1];
            int sleep;

            int.TryParse(args[2], out sleep);

            var request = new RequestModel
            {
                Encoding = encoding,
                Url = url,
                Timeout = 10000,
                UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36",
                Sleep = 2000,
                Depth = 1,
                Source = "下书网",
                Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
                //Connection = "keep-alive",
                Header = new List<KeyValue>()
                {
                    new KeyValue("Accept-Encoding","gzip"),
                    new KeyValue("Accept-Language","zh-CN,zh;q=0.9"),
                    new KeyValue("Cache-Control","max-age=0"),
                },
                Cookie = new List<KeyValue>()
                {

                }
            };

            return request;
        }


    }
}
